package com.ruoyi.zhmk.service;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.zhmk.domain.ZhmkAsk;
import com.ruoyi.zhmk.domain.ZhmkDoorDevice;
import com.ruoyi.zhmk.domain.ZhmkPerson;
import com.ruoyi.zhmk.domain.ZhmkUserinoutInfo;
import com.ruoyi.zhmk.domain.request.SendPersonRequest;
import com.ruoyi.zhmk.mapper.ZhmkAskMapper;
import com.ruoyi.zhmk.mapper.ZhmkDoorDeviceMapper;
import com.ruoyi.zhmk.mapper.ZhmkPersonMapper;
import com.ruoyi.zhmk.mapper.ZhmkUserinoutInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2021/6/25 16:07
 */
@Component
public class TaskService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskService.class);
    @Autowired
    private ZhmkPersonMapper zhmkPersonMapper;
    @Autowired
    private ZhmkDoorDeviceMapper zhmkDoorDeviceMapper;
    @Autowired
    private IZhmkPersonService zhmkPersonService;
    @Autowired
    private ZhmkUserinoutInfoMapper zhmkUserinoutInfoMapper;
    @Autowired
    private ZhmkAskMapper zhmkAskMapper;

    /**
     * 查询需要下发的数据，并下发
     */
    public void querySend(){
        LOGGER.info("请假下发数据执行");
        //查询出人员列表
        List<ZhmkPerson> zhmkPeople = zhmkPersonMapper.selectZhmkSendPerson();
        List<Long> personIds = new ArrayList<>();
        for (ZhmkPerson zhmkPerson : zhmkPeople) {
            personIds.add(zhmkPerson.getId());
            //修改人员状态为请假
            zhmkPerson.setUserStatus(1);
            zhmkPersonMapper.updateZhmkPerson(zhmkPerson);

        }
        //查询出设备列表
        List<ZhmkDoorDevice> zhmkDoorDevices = zhmkDoorDeviceMapper.selectZhmkDoorDeviceList(new ZhmkDoorDevice());
        List<Long> doorDeviceIds = new ArrayList<>();
        for (ZhmkDoorDevice zhmkDoorDevice : zhmkDoorDevices) {
            doorDeviceIds.add(zhmkDoorDevice.getId());
        }
        if(doorDeviceIds.size()>0 && personIds.size()>0){
            SendPersonRequest sendPersonRequest = new SendPersonRequest();
            sendPersonRequest.setDeviceIdList(doorDeviceIds);
            sendPersonRequest.setPersonIdList(personIds);
            AjaxResult ajaxResult = zhmkPersonService.personSend(sendPersonRequest);

            LOGGER.info("请假下发数据返回{}",ajaxResult);
        }


    }

    /**
     * 删除下发的内容
     */
    public void deleteSend(){
        LOGGER.info("请假时间已过删除下发数据执行");
        List<ZhmkPerson> zhmkPeople = zhmkPersonMapper.selectZhmkDeletePerson();
        List<Long> personIds = new ArrayList<>();
        for (ZhmkPerson zhmkPerson : zhmkPeople) {
            personIds.add(zhmkPerson.getId());
        }
        List<ZhmkDoorDevice> zhmkDoorDevices = zhmkDoorDeviceMapper.selectZhmkDoorDeviceList(new ZhmkDoorDevice());
        List<Long> doorDeviceIds = new ArrayList<>();
        for (ZhmkDoorDevice zhmkDoorDevice : zhmkDoorDevices) {
            doorDeviceIds.add(zhmkDoorDevice.getId());
        }
        if(doorDeviceIds.size()>0 && personIds.size()>0){
            SendPersonRequest sendPersonRequest = new SendPersonRequest();
            sendPersonRequest.setDeviceIdList(doorDeviceIds);
            sendPersonRequest.setPersonIdList(personIds);
            int i = zhmkPersonService.personDelete(sendPersonRequest);
        }
        List<ZhmkAsk> zhmkAsks = zhmkAskMapper.selectZhmkAskOverDue();
        //修改人员状态
        for (Long personId : personIds) {
            ZhmkPerson zhmkPerson = new ZhmkPerson();
            zhmkPerson.setId(personId);
            //下发状态，未下发
            zhmkPerson.setSendStatus(0);
            //判断是否需要修改人员在位状态
            boolean flag = true;
            for (ZhmkAsk zhmkAsk : zhmkAsks) {
                Long id = zhmkAsk.getId();
                if(personId.intValue() == id.intValue()){
                    flag = false;
                }
            }
            if(flag){
                //人员状态 在位
                zhmkPerson.setUserStatus(0);
            }

            zhmkPersonMapper.updateZhmkPerson(zhmkPerson);
        }

        //查询进门时间为null，并且请假结束时间小于当前时间的请假数据

        for (ZhmkAsk zhmkAsk : zhmkAsks) {
            //计算逾期时间
            Date askEndDate = zhmkAsk.getAskEndDate();
            //获取当前时间
            long currentTime = System.currentTimeMillis();
            long overdueTime = currentTime - askEndDate.getTime();
            zhmkAsk.setOverdueTime(overdueTime);
            zhmkAskMapper.updateZhmkAsk(zhmkAsk);


        }

        LOGGER.info("请假时间已过删除下发数据执行结束");
    }
}
